package com.briup.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.briup.cms.bean.Slideshow;
import com.briup.cms.dao.SlideshowDao;
import com.briup.cms.exception.ServiceException;
import com.briup.cms.service.ISlideshowService;
import com.briup.cms.util.ResultCode;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class SlideshowServiceImpl implements ISlideshowService {
    @Resource
    SlideshowDao slideshowDao;

    @Override
    public Slideshow queryById(Integer id) {
        return slideshowDao.selectById(id);
    }

    @Override
    public List<Slideshow> queryAllEnable() {
        LambdaQueryWrapper<Slideshow> slideshowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        slideshowLambdaQueryWrapper.eq(Slideshow::getStatus, "启用");
        return slideshowDao.selectList(slideshowLambdaQueryWrapper);
    }

    @Override
    public Page<Slideshow> queryAll(Integer pageNum, Integer pageSize, String status, String description) {
        Page<Slideshow> slideshowPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Slideshow> slideshowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        slideshowLambdaQueryWrapper.eq(StringUtils.hasText(status),Slideshow::getStatus, status).like
                (StringUtils.hasText(description),Slideshow::getDescription, description);
        slideshowDao.selectPage(slideshowPage, slideshowLambdaQueryWrapper);
        //return slideshowPage.getRecords();
        System.out.println("slideshowPage = " + slideshowPage);
        return slideshowPage;
    }

    @Override
    public void saveOrUpdate(Slideshow slideshow) {
        Integer id = slideshow.getId();
        String newUrl = slideshow.getUrl();
        LambdaQueryWrapper<Slideshow> slideshowLambdaQueryWrapper = new LambdaQueryWrapper<>();
        slideshowLambdaQueryWrapper.eq(Slideshow::getUrl, newUrl);
        Slideshow slideshowByUrl = slideshowDao.selectOne(slideshowLambdaQueryWrapper);

        if (id != null) {
            //修改操作
            Slideshow oldSlideShow = slideshowDao.selectById(id);
            if (oldSlideShow == null) {
                throw new ServiceException(ResultCode.DATA_NONE);
            }
            if (!oldSlideShow.getUrl().equals(newUrl) && slideshowByUrl.getUrl() != null) {
                throw new ServiceException(ResultCode.DATA_EXISTED);
            }
            slideshow.setUploadTime(LocalDateTime.now());
            slideshowDao.updateById(slideshow);
        } else {
            //新增操作
            if (slideshowByUrl != null)
                throw new ServiceException(ResultCode.DATA_EXISTED);
            slideshow.setUploadTime(LocalDateTime.now());
            slideshowDao.insert(slideshow);
        }

    }


    public void deleteInBatch(List<Integer> ids) {
        if (ids.size() == 1) {
            slideshowDao.deleteById(ids.get(0));
        }
        slideshowDao.deleteBatchIds(ids);
    }
}
